<?php

//	20090211
//	GetLangString updated
//	TODO display profile info
		
		if ($_REQUEST['profile_name']) {
			$profile_name = mb_strtolower($_REQUEST['profile_name'],"UTF-8");
			$query        = "SELECT * from users where name='" . $profile_name ."'";
			$result = mysql_query($query);
			if (mysql_num_rows($result)){ 
				$profile_user = mysql_fetch_array($result);				
//				$profile_id = mysql_result ($result,0);
				$profile_id = $profile_user['id'];
			}
		} 
		elseif ($_REQUEST['profile_id']){
			$profile_id   = mb_strtolower($_REQUEST['profile_id']  ,"UTF-8");
			$query        = "SELECT * from users where id='" . $profile_id ."'";
			$result = mysql_query($query);
			if (mysql_num_rows($result)){ 
				$profile_user = mysql_fetch_array($result);				
				$profile_name = $profile_user['name'];
				//$profile_name = mysql_result ($result,0);
			}
		}
		elseif ($id) {
			$profile_id   = $id;
			$profile_name = $name;
			$query        = "SELECT * from users where id='" . $profile_id ."'";
			$result = mysql_query($query);
			if (mysql_num_rows($result)){ 
				$profile_user = mysql_fetch_array($result);				
			}
		} 

		$query        = "SELECT * from info where id='$profile_id'";
		$result = mysql_query($query);

    $status = $profile_user['status'];
    if ($status & $STA_BANNED) {
      //$SITE_MIDDLE .= "This user is banned!"; // TODO: String in database!!!!
	  $SITE_MIDDLE .= FormatElement( GetLangString($lang, "txt_banned"), GetLangString($lang, "msg_banned" ));
    } 
    else
		if (mysql_num_rows($result)){ 
			$profileinfo = mysql_fetch_assoc($result);
				if ($id) {

						$img = "/index.php?do=getphoto&id=".$profile_user['photo']."&size=126";			

					$infotxt .= "<center><a href=\"index.php?do=photoalbum&profile_id=$profile_id\">
					<img src=\"$img\" border=\"0\"></a><br><a href=\"index.php?do=photoalbum&profile_id=$profile_id\">".GetLangString($lang,"txt_photoalbum")."</a><br>";



					// friends
					$query        = "SELECT * from friends where user1='$id' and user2='$profile_id'";
					$result = mysql_query($query);
					
					if (mysql_num_rows($result)){ 
							$query        = "SELECT * from friends where user2='$id' and user1='$profile_id'";
							$result = mysql_query($query);
							if (mysql_num_rows($result)){ 
								$infotxt .="<a href='index.php?do=friends'>". GetLangString($lang,"txt_friend") ."</a><br>";
							} else $infotxt .= GetLangString($lang,"txt_friendrequested")."<br>";
					} else {
						$query        = "SELECT * from friends where user2='$id' and user1='$profile_id'";
						$result = mysql_query($query);
						if (mysql_num_rows($result)){ 
							$infotxt .= "<a href=\"/index.php?do=friends&add=$profile_id\">".GetLangString($lang,"txt_acceptfriend")."</a><br>";
							} else
							 $infotxt .= "<a href=\"/index.php?do=friends&add=$profile_id\">".GetLangString($lang,"txt_friendrequest")."</a><br>";
					}
				$infotxt .= "</center><br>";

			//$infotxt .=GetLangString($lang,"txt_signupdate") ."<br>";
			//$infotxt .=date("d-m-Y",$profile_user['signup']) ."<br>";;
			$infotxt .="<div class=info align=left>".GetLangString($lang,"txt_signupdate")."</div>";
			$infotxt .="<div class=info align=right>".date("d-m-Y",$profile_user['signup'])."</div>";
			
			$query2 = "select `lastseen` from `login` where `id`='$profile_id' order by `lastseen` desc limit 1";
			$result2= mysql_query($query2);
			if (mysql_num_rows($result2)) $lastseen = date("d-m-Y", mysql_result($result2,0)) ; else  $lastseen= GetLangString($lang, "txt_unknown"); 
			$infotxt .="<div class=info align=left>".GetLangString($lang,"txt_lastseen")."</div>";
			$infotxt .="<div class=info align=right>$lastseen</div>";
			
		//	$infotxt .="<div class=info>".GetLangString($lang,"txt_bday")."</div><div class=info>{$profileinfo['day']}-{$profileinfo['month']}-{$profileinfo['year']}</div>" ;
			$age = date("Y") - $profileinfo['year'];
			if ( (date('n') < $profileinfo['month']) or ((date('n') == $profileinfo['month']) and (date('n') < $profileinfo['day'])) ) $age--;  


			$infotxt .="<div class=info align=left>".GetLangString($lang,"txt_gender")."</div>";
			$infotxt .="<div class=info align=right>".GetLangString($lang,$profileinfo['gender'])."</div>";
			
			$infotxt .="<div class=info align=left>".GetLangString($lang,"txt_orientation")."</div>";
			$infotxt .="<div class=info align=right>".GetLangString($lang,$profileinfo['orientation'])."</div>";

			$infotxt .="<div class=info align=left>".GetLangString($lang,"txt_status")."</div>";
			$infotxt .="<div class=info align=right>".GetLangString($lang,$profileinfo['status'])."</div>";

			$infotxt .="<div class=info align=left>".GetLangString($lang,"txt_age")."</div>";
			$infotxt .="<div class=info align=right>$age</div>";


			$infotxt .="<div class=info align=left>".GetLangString($lang,"txt_drink")."</div>";
			$infotxt .="<div class=info align=right>".GetLangString($lang,$profileinfo['drink'])."</div>";
			

			$infotxt .="<div class=info align=left>".GetLangString($lang,"txt_smoke")."</div>";
			$infotxt .="<div class=info align=right>".GetLangString($lang,$profileinfo['smoke'])."</div>";
			

			$infotxt .="<div class=info align=left>".GetLangString($lang,"txt_snus")."</div>";
			$infotxt .="<div class=info align=right>".GetLangString($lang,$profileinfo['snus'])."</div>";
			

			$infotxt .="<div class=info align=left>".GetLangString($lang,"txt_drugs")."</div>";
			$infotxt .="<div class=info align=right>".GetLangString($lang,$profileinfo['drugs'])."</div>";
			$SITE_RIGHT .= FormatSmallElement ( str_ireplace("%name%","<b>$profile_name</b>",GetLangString($lang,"txt_lookingat")), $infotxt);

//			if (!$RENDER_MOBILE) 
				{
				$query = "select distinct `visitor` from `profilevisitors` where `profile`='$profile_id' order by `time` desc limit 6";
			
				$result = mysql_query($query);
				while ($recentvisitor = mysql_fetch_array($result)){
					$visitorid = $recentvisitor['visitor'];
					$query2 = "SELECT * from `users` where `id`='$visitorid'";
					$result2 = mysql_query($query2);
					$visitorinfo = mysql_fetch_array($result2);
					$img = "/index.php?do=getphoto&id=".$visitorinfo['photo']."&size=33&square=1";			
					$visitorname  = $visitorinfo['name'];
					$visitors .= "<a href=\"/index.php?do=profile&profile_id=$visitorid\"><div class=\"userextrasmallthumb\">
										$visitorname<br><img src=\"$img\" title=\"$visitorname\" 
											width=\"33\" height=\"33\"></div></a>";
				
				}
				$SITE_RIGHT .= FormatSmallElement ( GetLangString($lang,"txt_recentvisitors"), "$visitors");
				
			}	
			
			$time = time();

			$query = "delete from `profilevisitors` where `profile`='$profile_id' and `visitor`= '$id'";
			$result = mysql_query($query);

			$query = "insert into `profilevisitors` (`profile`,`visitor`,`time`) values ('$profile_id','$id',$time)";
			$result = mysql_query($query);
							
			
		}
			
			
			$query        = "SELECT * from profile where id='" . $profile_id ."'";
			$result = mysql_query($query);
			if (mysql_num_rows($result)){ 
				$profiletext = mysql_fetch_assoc($result);
/*		
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_about") 	, 		bb2html($profiletext[about]));
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_like") 	,  		bb2html($profiletext[like]));
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_dislike"),	  	bb2html($profiletext[dislike]));	
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_music") 	,  		bb2html($profiletext[music]));
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_film") 	,  		bb2html($profiletext[film]));
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_book") 	,  		bb2html($profiletext[book]));
*/
				if (!$RENDER_MOBILE) $SITE_MIDDLE .= "<table width='100%'><tr><td width='50%' valign='top'>"; 
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_about") 	, 		bb2html($profiletext[about]));
				if (!$RENDER_MOBILE) $SITE_MIDDLE .="</td><td  width='50%' valign='top'>";
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_like") 	,  		bb2html($profiletext[like]));
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_dislike"),	  	bb2html($profiletext[dislike]));	
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_music") 	,  		bb2html($profiletext[music]));
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_film") 	,  		bb2html($profiletext[film]));
				$SITE_MIDDLE  .= FormatElement (GetLangString($lang,"txt_book") 	,  		bb2html($profiletext[book]));
				if (!$RENDER_MOBILE) $SITE_MIDDLE .="</td></tr>";
				if (!$RENDER_MOBILE) $SITE_MIDDLE .="<tr><td valign=top>";
				include ("lastfm.php");
				if (!$RENDER_MOBILE) $SITE_MIDDLE .="</td><td valign=top>";
				include ("mal.php");
				if (!$RENDER_MOBILE) $SITE_MIDDLE .="</td></tr>";
				if ($id) { 

					if (!$RENDER_MOBILE) {
						if (!$LOWRES) $SITE_MIDDLE .= "<tr><td  width='50%' valign='top'>";
						else          $SITE_MIDDLE .= "<tr><td colspan=2>";
					}
					include ("profilefriends.php");
					if (!$RENDER_MOBILE) {
						if (!$LOWRES) $SITE_MIDDLE .= "</td><td  width='50%' valign='top'>";
					}
					include ("profilephotos.php");
					if (!$RENDER_MOBILE) $SITE_MIDDLE .= "</td></tr><tr><td  colspan=2 width='100%' valign='top'>";
					include ("profilecomments.php");
					if (!$RENDER_MOBILE) $SITE_MIDDLE .= "</td></tr>";
				}
				if (!$RENDER_MOBILE) $SITE_MIDDLE .= "</table>";
				if (!$id) {
					$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_comments"),GetLangString($lang,"txt_pleaselogin"));
				}
			}

		} 

		
?>
